﻿using Microsoft.Extensions.Logging;
using ST.Common.Util;
using ST.Common.Util.Text;
using ST.Common.Util.Xml;
using ST.WX.Common.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Web;
using System.Xml;

namespace ST.WX.Common.Pay
{
	public class WXPayUnifiedOrderRet : WXPayRet
	{
		/// <summary>
		/// 设备号
		/// </summary>
		[StringLength(32)]
		public string device_info { get; set; }

		/// <summary>
		/// 交易类型（取值为：JSAPI，NATIVE，APP等）
		/// </summary>
		[StringLength(16)]
		public string trade_type { get; set; }

		/// <summary>
		/// 预支付交易会话标识（微信生成的预支付会话标识，用于后续接口调用中使用，该值有效期为2小时）
		/// </summary>
		[StringLength(64)]
		public string prepay_id { get; set; }

		/// <summary>
		/// 二维码链接（trade_type=NATIVE时有返回，此url用于生成支付二维码，然后提供给用户进行扫码支付。）
		/// </summary>
		[StringLength(64)]
		public string code_url { get; set; }

		protected override void ConvertToObj()
		{
			base.ConvertToObj();
			if (values.ContainsKey("device_info"))
			{
				device_info = Convert.ToString(values["device_info"]);
			}
			if (values.ContainsKey("trade_type"))
			{
				trade_type = Convert.ToString(values["trade_type"]);
			}
			if (values.ContainsKey("prepay_id"))
			{
				prepay_id = Convert.ToString(values["prepay_id"]);
			}
			if (values.ContainsKey("code_url"))
			{
				code_url = Convert.ToString(values["code_url"]);
			}
		}

		public override void Validate()
		{
			base.Validate();
			if (result_code != "SUCCESS")
			{
				throw new WXPayException("错误代码：" + err_code + " " + err_code_des);
			}
			if (string.IsNullOrEmpty(prepay_id))
			{
				throw new WXPayException("prepay_id为空");
			}
		}
	}
}
